package YunNotes.util.db;

import java.sql.Connection;
import java.sql.SQLException;

import static java.sql.DriverManager.getConnection;

/**
 * @author 连接数据库
 */
public class DBUtil {

    private static final String URL = "jdbc:mysql://123.57.3.195:3306/YDYP?characterEncoding=utf8&useSSL=true&serverTimezone=UTC";
    private static final String USER = "root";
    private static final String PASSWORD = "ZAMTYR1314";

    private static Connection conn;


    /**
     * 静态块
     *
     */
    static {
        try {
            //1.加载驱动程序
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获得数据库的连接
            conn = getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConn() {
        return conn;
    }

    public void closeConn(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
                System.out.println("成功关闭数据库连接");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
